import { Module } from 'vuex';
import { IDashboardState } from './types';
import { IRootState } from '../types';
import {
  getAmountGoodsList,
  getCategoryGoodsCount,
  getCategoryGoodsSale,
  getCategoryGoodsFavor,
  getAddressGoodsSale
} from '@/service/analysis/dashboard';
import type { Count, Sale, Favor, Address, Amount } from './types';
const dashboardModule: Module<IDashboardState, IRootState> = {
  namespaced: true,
  state: () => {
    return {
      categoryGoodsCount: [],
      categoryGoodsSale: [],
      categoryGoodsFavor: [],
      addressGoodsSale: [],
      amountGoodsList: []
    };
  },
  getters: {
    categoryGoodsSaleName(state) {
      const arr: string[] = [];
      for (const item of state.categoryGoodsSale) {
        arr.push(item.name);
      }
      return arr;
    },
    categoryGoodsSaleValue(state) {
      const arr: number[] = [];
      for (const item of state.categoryGoodsSale) {
        arr.push(item.goodsSale);
      }
      return arr;
    },
    categoryGoodsFavorValue(state) {
      const arr: number[] = [];
      for (const item of state.categoryGoodsFavor) {
        arr.push(item.goodsFavor);
      }
      return arr;
    },
    addressGoodsSaleValue(state) {
      return state.addressGoodsSale.map((item) => {
        return { name: item.address, value: item.count };
      });
    }
  },
  mutations: {
    saveAmountGoodsList(state, playload: Amount[]) {
      state.amountGoodsList = playload;
    },
    saveCategoryGoodsCount(state, playload: Count[]) {
      state.categoryGoodsCount = playload;
    },
    saveCategoryGoodsSale(state, playload: Sale[]) {
      state.categoryGoodsSale = playload;
    },
    savecategoryGoodsFavor(state, playload: Favor[]) {
      state.categoryGoodsFavor = playload;
    },
    saveAddressGoodsSale(state, playload: Address[]) {
      state.addressGoodsSale = playload;
    }
  },
  actions: {
    async getAmountGoodsList({ commit, dispatch }) {
      const res = await getAmountGoodsList();
      if (res.code === 1) {
        commit('saveAmountGoodsList', res.data);
        dispatch('getCategoryGoodsCount');
      }
    },
    async getCategoryGoodsCount({ commit, dispatch }) {
      const res = await getCategoryGoodsCount();
      if (res.code === 1) {
        commit('saveCategoryGoodsCount', res.data);
        dispatch('getCategoryGoodsSale');
      }
    },
    async getCategoryGoodsSale({ commit, dispatch }) {
      const res = await getCategoryGoodsSale();
      if (res.code === 1) {
        commit('saveCategoryGoodsSale', res.data);
        dispatch('getCategoryGoodsFavor');
      }
    },
    async getCategoryGoodsFavor({ commit, dispatch }) {
      const res = await getCategoryGoodsFavor();
      if (res.code === 1) {
        commit('savecategoryGoodsFavor', res.data);
        dispatch('getAddressGoodsSale');
      }
    },
    async getAddressGoodsSale({ commit }) {
      const res = await getAddressGoodsSale();
      if (res.code === 1) {
        commit('saveAddressGoodsSale', res.data);
      }
    }
  }
};
export default dashboardModule;
